package myJs.api

import myJs.myPkg.MyFetch
import myJs.tool.Tool
import org.scalajs.dom.FormData
import org.scalajs.macrotaskexecutor.MacrotaskExecutor.Implicits._
import shared.pojo.CompanyMember.CompanyMemberData
import shared.pojo.Pojo
import slinky.web.html.s
import upickle.default._

/**
 * Created by yz on 19/5/2022
 */
object CompanyMemberApi {

  import Pojo.companyMember._

  val baseUrl = s"${Api.apiUrl}/panda/companyMember"
  val adminUrl = s"${Api.apiUrl}/panda/admin/companyMember"

  def all = {
    val url = s"${Api.apiUrl}/panda/admin/companyMember/all"
    MyFetch.getRnStr(url = s"${url}", headers = Map("Content-Type" -> "application/json",
      "X-Authorization" -> s"Bearer ${Tool.getToken}")).map { str =>
      read[List[ujson.Obj]](str)
    }
  }

  def getByAccount(account: String) = {
    val url = s"${Api.apiUrl}/panda/companyMember/getByAccount"
    MyFetch.getRnStr(url = s"${url}?account=${account}", headers = Map("Content-Type" -> "application/json",
      "X-Authorization" -> s"Bearer ${Tool.getToken}")).map { str =>
      read[CompanyMemberData](str)
    }
  }

  def update(request: CompanyMemberData) = {
    val url = s"${Api.apiUrl}/panda/companyMember/update"
    MyFetch.postRnText(url = s"${url}", data = write(request), headers = Map("Content-Type" -> "application/json",
      "X-Authorization" -> s"Bearer ${Tool.getToken}")).map { str =>
      str
    }
  }

  def resetPassword(request: CompanyMemberData) = {
    val url = s"${baseUrl}/resetPassword"
    MyFetch.postRnText(url = s"${url}", data = write(request), headers = Map("Content-Type" -> "application/json",
      "X-Authorization" -> s"Bearer ${Tool.getToken}")).map { str =>
      str
    }
  }

  def add(request: Add.Request) = {
    val url = s"${adminUrl}/add"
    MyFetch.postRnText(url = s"${url}", data = write(request),
      headers = Map("X-Authorization" -> s"Bearer ${Tool.getToken}", "Content-Type" -> "application/json")).map { str =>
      str
    }
  }

  def getById(idV: String) = {
    val url = s"${Api.apiUrl}/panda/admin/companyMember/getById"
    MyFetch.getRnStr(url = s"${url}?id=${idV}", headers = Map("Content-Type" -> "application/json",
      "X-Authorization" -> s"Bearer ${Tool.getToken}")).map { str =>
      read[CompanyMemberData](str)
    }
  }

  def delete(id: String) = {
    val url = s"${Api.apiUrl}/panda/admin/companyMember/delete"
    MyFetch.deleteRnStr(url = s"${url}?id=${id}", headers = Map("Content-Type" -> "application/json",
      "X-Authorization" -> s"Bearer ${Tool.getToken}")).map { str =>
      str
    }
  }

  def allCompany = {
    val url = s"${baseUrl}/allCompany"
    MyFetch.getRnStr(url = s"${url}", headers = Map("Content-Type" -> "application/json")).map { str =>
      read[List[String]](str)
    }
  }

}
